package com.demo.delivery.sqlite;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.demo.delivery.entity.User;
import com.demo.delivery.utils.MD5Utils;

public class UserBusiness {

    /**
     * 根据用户名查询用户
     */
    public static BusinessResult<User> queryByUsername(String username) {
        if (TextUtils.isEmpty(username)) {
            return new BusinessResult<>(false, "用户名不能为空", null);
        }
        DatabaseHelper instance = DatabaseHelper.getInstance();
        SQLiteDatabase db = instance.getReadableDatabase();
        String sql = "SELECT * FROM user WHERE username=?";
        String[] args = new String[]{username};
        User result = null;
        try (Cursor cursor = db.rawQuery(sql, args)) {
            if (cursor.moveToNext()) {
                result = new User();
                result.setId(cursor.getInt(0));
                result.setUsername(cursor.getString(1));
                result.setPassword(cursor.getString(2));
            }
        }
        if (result == null) {
            return new BusinessResult<>(false, "用户不存在", null);
        }
        return new BusinessResult<>(true, "用户已存在", result);
    }

    /**
     * 注册
     */
    public static BusinessResult<User> register(User user) {
        if (TextUtils.isEmpty(user.getUsername())) {
            return new BusinessResult<>(false, "用户名不能为空", null);
        }
        if (TextUtils.isEmpty(user.getPassword())) {
            return new BusinessResult<>(false, "密码不能为空", null);
        }
        BusinessResult<User> queryByUsernameResult = queryByUsername(user.getUsername());
        if (queryByUsernameResult.isSuccess()) {
            return new BusinessResult<>(false, "用户已存在", null);
        }
        DatabaseHelper instance = DatabaseHelper.getInstance();
        SQLiteDatabase db = instance.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", user.getUsername());
        values.put("password", MD5Utils.md5(user.getPassword()));
        long result = db.insert("user", null, values);
        if (result == -1) {
            return new BusinessResult<>(false, "注册失败", null);
        }
        return new BusinessResult<>(true, "注册成功", user);
    }

    /**
     * 登录
     */
    public static BusinessResult<User> login(User user) {
        if (TextUtils.isEmpty(user.getUsername())) {
            return new BusinessResult<>(false, "用户名不能为空", null);
        }
        if (TextUtils.isEmpty(user.getPassword())) {
            return new BusinessResult<>(false, "密码不能为空", null);
        }
        BusinessResult<User> queryByUsernameResult = queryByUsername(user.getUsername());
        if (!queryByUsernameResult.isSuccess()) {
            return new BusinessResult<>(false, "用户不存在", null);
        }
        User result = queryByUsernameResult.getData();
        if (!result.getPassword().equals(MD5Utils.md5(user.getPassword()))) {
            return new BusinessResult<>(false, "密码错误", null);
        }
        return new BusinessResult<>(true, "登录成功", result);
    }
}
